package org.abratuhi.kvngroupware.registration.mptua;

import org.abratuhi.kvngroupware.network.AbstractRequestMessageProceeder;
import org.abratuhi.kvngroupware.network.Client;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.server.db.ServerDB;
import org.abratuhi.kvngroupware.team.manager.TeamManager;
import org.abratuhi.kvngroupware.team.model.Team;
import org.abratuhi.kvngroupware.user.manager.UserManager;
import org.abratuhi.kvngroupware.user.model.User;
import org.jdom.Element;

public class RegistrationRequestMessageProceeder extends AbstractRequestMessageProceeder{
	
	MessageSender sender;
	UserManager usermanager;
	TeamManager teammanager;
	ServerDB db;
	
	public RegistrationRequestMessageProceeder(MessageSender sender, UserManager usermanager, TeamManager teammanager, ServerDB db){
		this.sender = sender;
		this.usermanager = usermanager;
		this.teammanager = teammanager;
		this.db = db;
	}

	@Override
	public void proceedMessage(Client client, Message message) {
		Element request = message.getRoot().getChild("request");
		String type = request.getAttributeValue("type");
		String action = request.getAttributeValue("action");
		
		if(type.equals("user")){
			User u = (request.getChild("user")!=null) ? User.fromElement((Element) request.getChild("user")) : null;
			
			String[] attrNames = new String[]{"type", "action", "status"};
			String[] attrValues= new String[]{type, action, "ok"};
			String text = new String();
			Element[] content = new Element[]{u.toElement()};
			Message responseMessage;
			
			if(action.equals("create")){
				boolean okLogin = db.containsLogin(u.getId());
				if(!okLogin){
					db.addUser(u.toElement());
				}
				else{
					attrValues[2] = "invalid_id";
				}
			}
			else if(action.equals("update")){
				// TODO
			}
			else if(action.equals("delete")){
				// TODO
			}

			responseMessage = sender.createMessage(attrNames, attrValues, text, content);
			sender.sendMessage(responseMessage);
		}
		else if(type.equals("team")){
			String[] attrNames = new String[]{"type", "action", "status"};
			String[] attrValues= new String[]{type, action, "ok"};
			String text = new String();
			Element[] content = new Element[0];
			Message responseMessage;
			
			Team t = request.getContentSize()>0? Team.fromElement(request.getChild("team")):null;
			if(t!=null) content = new Element[]{t.toElement()};
			
			if(action.equals("create")){
				if(!teammanager.contains(t.getId())){
					teammanager.put(t.getId(), t);
					db.addTeam(t.toElement());
				}
				else{
					attrValues[2] = "used_id";
				}
			}
			else if(action.equals("get")){
				content = db.getTeams();
			}
			else if(action.equals("update")){
				//TODO
			}
			else if(action.equals("delete")){
				//TODO
			}
			
			responseMessage = sender.createMessage(attrNames, attrValues, text, content);
			//sender.sendMessage(responseMessage);
			client.sendMessage(responseMessage);
		}
	}

}
